package com.songlanyun.modules.config.service.impl;

import java.util.Map;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.utils.BeanUtil;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;
import com.songlanyun.modules.config.dao.PointGradeConfigDao;
import com.songlanyun.modules.config.entity.PointGradeConfigEntity;
import com.songlanyun.modules.config.model.PointGradeConfigDTO;
import com.songlanyun.modules.config.service.PointGradeConfigService;

import cn.hutool.core.util.ObjectUtil;
import org.springframework.transaction.annotation.Transactional;

/**
 * 积分配置-等级配置
 *
 * @author lijiahe
 * @email
 * @date 2022-09-21 14:58:22
 */
@Service("pointGradeConfigService")
public class PointGradeConfigServiceImpl extends ServiceImpl<PointGradeConfigDao, PointGradeConfigEntity>
    implements PointGradeConfigService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PointGradeConfigEntity> page = this.page(new Query<PointGradeConfigEntity>().getPage(params),
            new LambdaQueryWrapper<PointGradeConfigEntity>());
        return new PageUtils(page);
    }

    @Override
    public PointGradeConfigDTO info() {
        return baseMapper.info();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdatePointGrade(PointGradeConfigDTO dto) {
        if(ObjectUtil.isNull(dto)) {
            return;
        }
        PointGradeConfigDTO info = this.info();
        PointGradeConfigEntity entity = new PointGradeConfigEntity();
        BeanUtil.copyPropertiesIgnoreNull(dto, entity);
        if (ObjectUtil.isNull(info)) {
            baseMapper.insert(entity);
        } else {
            entity.setId(info.getId());
            baseMapper.updateById(entity);
        }
    }

}
